package com.daycomtech.jgooglechart.charts;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.daycomtech.jgooglechart.dataobjects.JGChartAxis;
import com.daycomtech.jgooglechart.dataobjects.JGChartData;
import com.daycomtech.jgooglechart.exception.JGoogleChartException;
import com.daycomtech.jgooglechart.util.JGChartCallerUtil;


public abstract class JGChart
{

	private int width=0;
	private int height=0;

	private String type;

	private float[] data;
	private String[] labels;
	private String[] colors;

	private ArrayList<JGChartAxis> chartAxis = null;


	private float dataScalingMinValue=0;
	private float dataScalingMaxValue=0;

	private String gridLines="";

    public JGChart(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public JGChart( int height)
    {
    	this.height = height;

    	double width = 2.5 * height;
        this.width = (int)Math.rint(width);

        System.out.println("WIDTH:::"+this.width);
    }

	public JGChart()
	{

	}

    abstract public String getUrlString() throws UnsupportedEncodingException;


    public void setChartDatas(ArrayList<JGChartData> chartDatas)
    {
    	data = new float[chartDatas.size()];
    	labels = new String[chartDatas.size()];
    	colors = new String[chartDatas.size()];

    	int i = 0;
    	for (JGChartData chartData: chartDatas)
    	{
    		labels[i]=chartData.getLabel();
    		data[i]=chartData.getValue();
    		colors[i]=chartData.getColor();
    		i++;
    	}
    }

    public Image getImage() throws IOException, JGoogleChartException
    {
    	return JGChartCallerUtil.getImage(this.getUrlString());
    }

    public void saveToFile(String path, String imageFormat) throws IOException, JGoogleChartException
    {
    	BufferedImage bimage = (BufferedImage)this.getImage();
    	ImageIO.write(bimage, imageFormat, new File(path));
    }
	public int getWidth()
	{
		return width;
	}
	public void setWidth(int width)
	{
		this.width = width;
	}
	public int getHeight()
	{
		return height;
	}
	public void setHeight(int height)
	{
		this.height = height;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public float[] getData() {
		return data;
	}

	public void setData(float[] data) {
		this.data = data;
	}

	public String[] getLabels() {
		return labels;
	}

	public void setLabels(String[] labels) {
		this.labels = labels;
	}

	public String[] getColors() {
		return colors;
	}

	public void setColors(String[] colors) {
		this.colors = colors;
	}

	public ArrayList<JGChartAxis> getChartAxis() {
		return chartAxis;
	}

	public void setChartAxis(ArrayList<JGChartAxis> chartAxis) {
		this.chartAxis = chartAxis;
	}

	public float getDataScalingMinValue() {
		return dataScalingMinValue;
	}

	public void setDataScalingMinValue(float dataScalingMinValue) {
		this.dataScalingMinValue = dataScalingMinValue;
	}

	public float getDataScalingMaxValue() {
		return dataScalingMaxValue;
	}

	public void setDataScalingMaxValue(float dataScalingMaxValue) {
		this.dataScalingMaxValue = dataScalingMaxValue;
	}

	public String getGridLines() {
		return gridLines;
	}

	public void setGridLines(String gridLines) {
		this.gridLines = gridLines;
	}


}
